//获取数据信息
var param2 = "/agency/fiExtractProfit/showbalance";
var method2 = "post";
var datas2 = "";
getAgentMsg(param2, method2, datas2);
$(".accountBalance").html(dataResult.data.accountBalance);
$(".leftProfit").html(dataResult.data.leftProfit);
$(".leftIncome").html(dataResult.data.leftIncome);

//分页方法
function paging(page,pageSize,total){
	page = Number(page);
	
	//最大页数
	var maxPage = Math.ceil(total/pageSize) || 1;
	if(page>maxPage){
		//取值不能超过maxPage
	    page = Math.min(page,maxPage);
	    tiaozhuan(page);
	}else if(page<1){
		//取值不能小于1
	    page = Math.max(page,1);
	    tiaozhuan(page);
	}else{
		//	当前页数
	$('.ui-paging-count').val(page);
	//首页的置灰处理
	$('.js-page-first').removeClass('ui-pager-disabled');
	//上一页
	$('.js-page-prev').removeClass('ui-pager-disabled').attr("data-page",page-1);
	//下一页
	$('.js-page-next').removeClass('ui-pager-disabled').attr("data-page",page+1);
	//最末页
	$('.js-page-last').removeClass('ui-pager-disabled').attr("data-page",maxPage);
	//总页数显示
	$('.js-page-total').text(maxPage);
	
	
	//当前显示多少页的处理
	$(".ui-select-pagesize").val(pageSize);
	//第一页的处理
	if(page == 1){
		$(".js-page-first").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-prev").addClass('ui-pager-disabled').attr("data-page","");
	}
	if(page == maxPage){
		//最后一页的处理
		$(".js-page-last").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-next").addClass('ui-pager-disabled').attr("data-page","");
	}
	}
	 
	
};
//下拉选择分页条数的操作
$('.ui-select-pagesize').on("change",function(){
	pageSize = $(this).val();	
	tiaozhuan(p);
});

//跳转到X页  blur
$(".ui-paging-count").on("blur",function(){
	p = $(this).val();
	tiaozhuan(p);
});

//上一页，下一页，首页，最末页的跳转
$('.ui-pager').on("click",function(){
	p = $(this).attr("data-page");
	tiaozhuan(p);
});

var p=1;
function tiaozhuan(p){
	var adname=$(".adname").val();
	var pageSize=$('.ui-select-pagesize').val();
	var d={};
	d.pageSize=pageSize;
	d.adname=adname;
	d.page=p;
	$.ajax({
		url: url + "/agency/fiExtractProfit/selectByAdname",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		contentType: "application/json",
		beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				var data=result.data.rows;
				var html="";
				for(var i in data){
					html += '<tr><td>'+data[i].id+'</td><td>'+data[i].adComapyName+'</td>';
					html += '<td>'+data[i].allBiddingProfit+'</td><td>'+data[i].cptNoProfit+'</td>';
					html += '<td>'+data[i].cptProfit+'</td><td>'+data[i].allIncome+'</td>';
					html += '<td>'+data[i].noProfitIncome+'</td><td>'+data[i].profitIncome+'</td><td>';
					html += '<a  href="profitDetail.html?token=' + token + '&id=' + token_id + '&adverId=' + data[i].id + '&type=2">查看记录</a>&nbsp;&nbsp;&nbsp;&nbsp;';
					html += '<a onclick="showProfits('+data[i].id+',5,'+data[i].cptNoProfit+')" class="ng-scope">提取竞价交易利润</a>&nbsp;&nbsp;&nbsp;&nbsp;';
					html += '<a onclick="showProfits('+data[i].id+',6,'+data[i].noProfitIncome+')" class="ng-scope">提取保价保量收入</a></td></tr>';
				}
				$(".list_profit").html(html);
				var r=result.data.total;
					paging(p,pageSize,r);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	}); 
}
tiaozhuan(1);
//下载文件
function download(){
	var param2 = "/agency/fiExtractProfit/selectByAdname";
	var method2 = "post";
	var adname=$(".adname").val();
	var datas2 = {page: p, pageSize: 1000,adname:adname};
	getAgentMsg2(param2,method2,datas2);
	exportCsv({
        title: ['广告主ID','广告主','总竞价交易利润','竞价交易未提取利润','竞价交易已提取利润','总保价保量收入','保价保量未提取收入','保价保量已提取收入'],
        titleForKey: ['id','adComapyName','allBiddingProfit','cptNoProfit','cptProfit','allIncome','noProfitIncome','profitIncome'],
        data: dataResult2.data.rows,
    },"利润提取.csv");
}
//搜索按钮
$(".adname_btn").on("click",function(){
	p=1;
	tiaozhuan(p);
})


function showProfits(id,type,money) {
	$(".profitsObj_num").html(money);
	$(".amount_btn").attr("data",type).attr("data1",id);
	$(".profitsObj_money").val("");
    if(type == '5') {
        var popConfig = "利润提取";
        $(".profitsObj_money").attr("placeholder","请输入提取利润金额(元)");
        $(".profits_title").html("利润提取:");
    } else {
        var popConfig = "收入提取";
        $(".profitsObj_money").attr("placeholder","请输入提取收入金额(元)");
        $(".profits_title").html("收入提取:");
    }
    layer.open({
        type: 1,
        title: popConfig,
        content: $('.profits'),
        area:["422px","250px"],
		end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
			$(".profits").css("display","none");
		}
  	})
}

$(document).on("click",".close-layer",function () {
   layer.closeAll();
});

//利润提取按钮
var ppll_profits=true;
$(".amount_btn").on("click",function(){
	if(ppll_profits){
		ppll_profits=false;
		var amount=$(".profitsObj_money").val();
		if(!amount){
			layer.msg("请输入提取利润金额");
			ppll_profits = true;
			return false;
		}
		var d={};
		d.balance=amount;
		d.type=$(this).attr("data");
		d.adUserId=$(this).attr("data1");
		$.ajax({
			url: url + "/agency/fiExtractProfit/extract",
			type: 'POST',
			//		参数需转为字符串
			data: JSON.stringify(d),
			//		json跨域
			dataType: 'json',
			contentType: "application/json",
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result) {
				if(result.code == '0') {
					layer.msg(result.msg,{time:2*1000},function() {
						p=1;
						tiaozhuan(p);
						layer.closeAll();
					});
				} else {
					isErr(result);
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			},
			complete:function(){
				ppll_profits = true;
			}
		});
	}
})